<script lang="ts">
export default {
  name: "root_config"
}
</script>
<template>
  <div>
    <t-space direction="vertical" style="width: 100%">
      <div>
        <t-space break-line>
          <t-button @click="GetSystemConfig" size="small" variant="outline" theme="primary">
            <template #icon>
              <RefreshIcon/>
            </template>
            刷新
          </t-button>
          当前系统状态：
          <t-tag theme="primary" variant="light">总协程量：{{ data.config.get("总协程量：") }}</t-tag>
          <t-tag theme="primary" variant="light">编译时间：{{ data.config.get("当前程序的编译时间为：") }}</t-tag>
          <t-tag theme="primary" variant="light">
            心跳时间：
            {{ toLocalTimeByTimeSecond(Number(data.config.get("程序心跳时间：")) || 0) }}
          </t-tag>
        </t-space>
      </div>
      <div>
        <div>
          <t-space>
            内存使用量：
            <t-tag theme="primary" variant="light">内存容量：{{ data.config.get("内存容量：") }}</t-tag>
            <t-tag theme="primary" variant="light">内存已用：{{ data.config.get("内存已用：") }}</t-tag>
            <t-tag theme="primary" variant="light">内存剩余：{{ data.config.get("内存剩余：") }}</t-tag>
          </t-space>
          <t-progress :percentage="Number(data.config.get('内存已用%：'))"/>
        </div>
        <div>
          <t-space>
            硬盘使用量：
            <t-tag theme="primary" variant="light">硬盘容量：{{ data.config.get("硬盘容量：") }}</t-tag>
            <t-tag theme="primary" variant="light">硬盘已用：{{ data.config.get("硬盘已用：") }}</t-tag>
            <t-tag theme="primary" variant="light">硬盘剩余：{{ data.config.get("硬盘剩余：") }}</t-tag>
          </t-space>
          <t-progress :percentage="Number(data.config.get('硬盘已用%：'))"/>
        </div>
      </div>
      <t-card title="系统变量">
        <t-space direction="vertical">
          <t-input-adornment prepend="登录账号 token 有效期（范围 1800 ~ 86400）" append="秒">
            <t-input-number min="1800" max="86400" v-model="data.items.token_second"/>
          </t-input-adornment>
        </t-space>
        <div style="margin: 1em 0;width: 25em">
          <t-button block theme="success" size="large" @click="SaveSystemConfig" :disabled="data.loading">
            保存修改
          </t-button>
        </div>
      </t-card>
    </t-space>
  </div>
</template>

<script lang="ts" setup>
import {onMounted, reactive} from "vue";
import api from "../../../api/api";
import {toLocalTimeByTimeSecond} from "../../../tool/time";
import {LoadingPlugin, MessagePlugin} from "tdesign-vue-next";
import {RefreshIcon} from "tdesign-icons-vue-next";

const data = reactive({
  loading: false,
  config: new Map(),
  items: {
    token_second: 0,
  }
})
onMounted(async () => {
  await GetSystemConfig()
})

const GetSystemConfig = async () => {
  data.loading = true
  let res = await api.Root.GetSystemConfig()
  if (res != null) {
    data.config = new Map(Object.entries(res.data))
    data.items.token_second = Number(data.config.get("登录Token有效时间（秒）：")) || 0
  }
  data.loading = false
}

const SaveSystemConfig = async () => {
  LoadingPlugin(true)
  let res = await api.Root.SaveSystemConfig({
    token_second: data.items.token_second,
  })
  if (res != null) {
    await MessagePlugin.success('保存成功')
    await GetSystemConfig()
  }
  LoadingPlugin(false)
}
</script>